package collection.list;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 *
 * 自定义队列，并且实现节点反转
 * @Author Antony
 * @Since 2018/6/17 16:14
 */
public class MyLinkedList<E> implements List<E>{

    private int size;

    private Node first;

    private Node last;


    void linkLast(E e){
        final Node l = last;
        final Node newNode = new Node(e,l,null);
        last = newNode;
        if(l == null){
            first = newNode;
        }else{
            l.nextNode = newNode;
        }
        size++;
    }

    /**
     * 链表反转
     */
    public void reverse(){
        if(size <= 1){
            return;
        }

        Node current = first;
        Node temp;
        Node next;

        // 反转双链表节点指针
        while(current != null){
            next = current.nextNode;    //临时保存下一个节点

            temp = current.preNode;
            current.preNode = current.nextNode;
            current.nextNode = temp;

            current = next;
        }

        // 调换首尾节点
        temp = first;
        first = last;
        last = temp;

    }

    @Override
    public String toString() {
        Iterator<E> itr = iterator();
        if(!itr.hasNext()){
            return "MyLinkedList[]";
        }

        StringBuilder bu = new StringBuilder("MyLinkedList[");
        for(;;){
            E nextValue = itr.next();
            if(itr.hasNext()){
                bu.append(nextValue + ", ");
            }else{
                bu.append(nextValue + "]");
                return bu.toString();
            }
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size != 0;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<E> iterator() {
        return listIterator();
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T> T[] toArray(T[] a) {
        return null;
    }

    @Override
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        return false;
    }

    @Override
    public void clear() {

    }

    @Override
    public E get(int index) {
        return null;
    }

    @Override
    public E set(int index, E element) {
        return null;
    }

    @Override
    public void add(int index, E element) {

    }

    @Override
    public E remove(int index) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<E> listIterator() {
        return new MyIterator<>(first);
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return null;
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return null;
    }


    private static class MyIterator<E> implements ListIterator<E>{

        private Node<E> next;

        public MyIterator(Node<E> next) {
            this.next = next;
        }

        @Override
        public boolean hasNext() {
            return next != null;
        }

        @Override
        public E next() {
            E value = next.value;
            next = next.nextNode;
            return value;
        }

        @Override
        public boolean hasPrevious() {
            return next.hasPre();
        }

        @Override
        public E previous() {
            E value = next.preNode.value;
            next = next.preNode;
            return value;
        }

        @Override
        public int nextIndex() {
            return 0;
        }

        @Override
        public int previousIndex() {
            return 0;
        }

        @Override
        public void remove() {

        }

        @Override
        public void set(E e) {

        }

        @Override
        public void add(E e) {

        }
    }


    /**
     * 内节点对象
     * @param <E>
     */
    private static class Node<E>{

        E value;
        Node<E> preNode;
        Node<E> nextNode;

        boolean hasNext(){
            return nextNode != null;
        }

        boolean hasPre(){
            return preNode != null;
        }

        public Node(E value, Node preNode, Node nextNode) {
            this.value = value;
            this.preNode = preNode;
            this.nextNode = nextNode;
        }
    }
}
